package Servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import Dao.*;
import bean.medicine;
@WebServlet(urlPatterns = "/medicineServlet")

public class medicineServlet extends BaseServlet{
    medicineDao medicineDao = new medicineDao();
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<medicine> medicineList = medicineDao.queryMedicines();
        req.setAttribute("medicineList",medicineList);
        req.getRequestDispatcher("/orderMedicine.jsp").forward(req,resp);
    }

    protected void searchByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        medicine medicine = medicineDao.queryMedicineByName(name);
        req.setAttribute("medicine",medicine);
        req.getRequestDispatcher("/orderMedicine.jsp").forward(req,resp);

    }

    protected void searchByCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("searchByCategory");
        String category = req.getParameter("category");
        List<medicine> medicineListCategory = medicineDao.queryMedicineByCategory(category);
        for (medicine medicine : medicineListCategory) {
            System.out.println(medicine);
        }
        req.setAttribute("medicineListCategory",medicineListCategory);
        req.getRequestDispatcher("/orderMedicine.jsp").forward(req,resp);
    }
}
